﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using BusinessObjects;
using TcpLib;

namespace YourWorldServer
{
    public class PalyersCommunicator
    {
        private static PalyersCommunicator instance;

        private PalyersCommunicator()
        {
        }

        public static PalyersCommunicator Instance
        {
            get
            {
                if ( instance == null )
                    instance = new PalyersCommunicator();

                return instance;
            }
        }

        public void ChatMessageReceived( ConnectionState payload, int bytesRead )
        {
            string xmlData = Encoding.UTF8.GetString( payload.Buffer, 1, bytesRead - 1 );
            XmlSerializer xmlSrl = new XmlSerializer( typeof( ChatMessage ) );
            StringReader stream = new StringReader( xmlData );

            ChatMessage gameMessage = xmlSrl.Deserialize( stream ) as ChatMessage;
            Game game = GameManager.Instance.GetGame( gameMessage.SenderMapId );

            //Debug.Assert( game != null );

            foreach ( Player player in game.Players )
            {
                ConnectionState comm = GameManager.Instance.GetPlayerCommunicationChannel( player.Id );
                comm.Write( payload.Buffer, 0, bytesRead );
            }
        }

        public void PlayerConnectionRequest( ConnectionState payload, int bytesRead )
        {
            string xmlData = Encoding.UTF8.GetString( payload.Buffer, 1, bytesRead - 1 );
            XmlSerializer xmlSrl = new XmlSerializer( typeof( PlayerConnectionMessage ) );
            StringReader stream = new StringReader( xmlData );

            PlayerConnectionMessage connectMessage = xmlSrl.Deserialize( stream ) as PlayerConnectionMessage;
            ConnectionState target = GameManager.Instance.GetPlayerCommunicationChannel( connectMessage.TargetId );

            //Debug.Assert( target != null );

            if ( target != null )
                target.Write( payload.Buffer, 0, bytesRead );
        }

        public void PlayerConnectionResponse( ConnectionState payload, int bytesRead )
        {
            string xmlData = Encoding.UTF8.GetString( payload.Buffer, 1, bytesRead - 1 );
            XmlSerializer xmlSrl = new XmlSerializer( typeof( PlayerConnectionMessage ) );
            StringReader stream = new StringReader( xmlData );

            PlayerConnectionMessage connectMessage = xmlSrl.Deserialize( stream ) as PlayerConnectionMessage;

            Player sender = GameManager.Instance.GetPlayer( connectMessage.SenderId );
            Player target = GameManager.Instance.GetPlayer( connectMessage.TargetId );

            sender.Connections.Add( connectMessage.TargetId );
            target.Connections.Add( connectMessage.SenderId );

            ConnectionState senderStream = GameManager.Instance.GetPlayerCommunicationChannel( connectMessage.SenderId );

            //Debug.Assert( sender != null );

            if ( sender != null )
                senderStream.Write( payload.Buffer, 0, bytesRead );
        }
    }
}
